﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Threading;

namespace SQLComparer
{
    public partial class FormConnection : Form
    {

        public FormConnection()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {

            Connection conn = new Connection();
            if (txtName.Text.Trim() == "")
            {
                MessageBox.Show("lack of name");
                txtName.Focus();
                return;
            }
            conn.Name = txtName.Text.Trim();
            conn.Server = txtServer.Text.Trim();
            conn.Database = txtDatabase.Text.Trim();
            conn.UserID = txtUserID.Text.Trim();
            conn.Password = txtPassword.Text.Trim();
            conn.ConnectionString = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", txtServer.Text.Trim(), txtDatabase.Text.Trim(), txtUserID.Text.Trim(), txtPassword.Text.Trim());
            List<Connection> list = Connection.Deserialize();
            list.Add(conn);
            Connection.Serialize(list);

            LoadData();
        }
        private void LoadData()
        {
            listBox1.Items.Clear();
            listBox1.DisplayMember = "Name";
            foreach (Connection conn in Connection.Deserialize())
            {

                int i = listBox1.Items.Add(conn);
                if (DBManager.ConnectionA != null && conn.ConnectionString == DBManager.ConnectionA.ConnectionString)
                {
                    this.listBox1.SelectedIndex = i;
                }
            }

            listBox2.Items.Clear();
            listBox2.DisplayMember = "Name";
            foreach (Connection conn in Connection.Deserialize())
            {
                int i = listBox2.Items.Add(conn);
                if (DBManager.ConnectionB != null && conn.ConnectionString == DBManager.ConnectionB.ConnectionString)
                {
                    this.listBox2.SelectedIndex = i;
                }

            }
        }

        private void FormConnection_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            {
                return;
            }
            DBManager.ConnectionA = (Connection)listBox1.SelectedItem;

        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (listBox2.SelectedItem == null)
            {
                return;
            }
            DBManager.ConnectionB = (Connection)listBox2.SelectedItem;

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Yes;
        }



    }
}
